package com.game.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.game.modal.User;

@Mapper
public interface UserMapper {
	
	@Insert("INSERT INTO user(openid,nick_name,avatar_url) VALUES(#{openid},#{nickName},#{avatarUrl})")
	int insert(User user);
	
	@Select("SELECT\n" +
			"   u.openid,\n" +
			"	u.avatar_url,\n" +
			"	u.nick_name,\n" +
			"	u.id\n" +
			"FROM\n" +
			"	user u\n "+
			" WHERE u.openid=#{openid}")
	User selectByPrimary(String openid);
	
	@Select("SELECT\n" +
			"   u.openid,\n" +
			"	u.avatar_url,\n" +
			"	u.nick_name,\n" +
			"	u.room_id,\n" +
			"	u.id\n" +
			"FROM\n" +
			"	user u\n "+
			"JOIN user_room ur ON ur.user_id = u.openid\n"+
			"WHERE ur.room_id=#{roomNo}")
	List<User> list(String roomNo);

	@Update("UPDATE user SET nick_name=#{nickName},avatar_url=#{avatarUrl} WHERE openid=#{openid}")
	int update(User user);
	
	@Insert("INSERT INTO user_room(user_id,room_id) VALUES(#{uid},#{roomid})")
	int insertUserRoom(@Param("uid")String uid,@Param("roomid")String roomid);
	
	@Select("SELECT count(1) FROM user_room u WHERE u.room_id=#{roomid} and u.user_id=#{uid}")
	int hasExsistUser(@Param("uid")String uid,@Param("roomid")String roomid);
}
